#include "HomePage.h"

HomePage::HomePage(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建水平布局用于放置搜索框和排序下拉框
    QHBoxLayout *searchLayout = new QHBoxLayout();

    // 创建搜索框
    searchBox = new QLineEdit(this);
    searchBox->setPlaceholderText("搜索");
    searchBox->setFixedHeight(40);

    // 创建排序下拉框
    sortComboBox = new QComboBox(this);
    sortComboBox->addItem("综合");
    sortComboBox->addItem("最新发布");
    sortComboBox->addItem("最多点赞");
    sortComboBox->setFixedHeight(40);
    sortComboBox->setFixedWidth(120); // 设置固定宽度

    sortComboBox->setStyleSheet(R"(
        QComboBox {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f9f9f9, stop:1 #e0e0e0);
            border: 1px solid #bbbbbb;
            border-radius: 6px;
            padding: 5px;
            font-size: 14px;
        }
        QComboBox:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffffff, stop:1 #d9d9d9);
        }
        QComboBox:focus {
            border: 1px solid #8abeff;
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffffff, stop:1 #eef6ff);
        }
        QComboBox::drop-down {
            border: 0px;
            padding-right: 5px;
        }
        QComboBox QAbstractItemView {
            outline: 0px;
            background-color: #ffffff;
            selection-background-color: #a6cfff;
        }
    )");
    QString searchStyle = R"(
        QLineEdit {
            padding-left: 10px;
            border: 1px solid #ccc;
            border-radius: 10px;
            font-size: 14px;
        }
        QLineEdit:focus {
            border: 1px solid #4A90E2;
        }
    )";
    searchBox->setStyleSheet(searchStyle);

    searchLayout->addWidget(searchBox);
    searchLayout->addWidget(sortComboBox);

    mainLayout->addLayout(searchLayout); // 搜索框位于最顶部

    // 切换按钮
    QWidget *buttonContainer = new QWidget(this);
    QHBoxLayout *buttonLayout = new QHBoxLayout(buttonContainer);

    buttonLayout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 左对齐 + 垂直居中

    QPushButton *recommend = new QPushButton("全部", this);
    QPushButton *sea = new QPushButton("理学工学农学", this);
    QPushButton *cs = new QPushButton("计算机", this);
    QPushButton *em = new QPushButton("经济管理", this);
    QPushButton *fl = new QPushButton("外语", this);
    QPushButton *ma = new QPushButton("音乐与艺术", this);
    QPushButton *psy = new QPushButton("心理学", this);
    QPushButton *hum = new QPushButton("文史哲法", this);
    QPushButton *mh = new QPushButton("医学与保健", this);

    recommend->setFixedSize(BtnWidth, BtnHeight);
    sea->setFixedSize(BtnWidth, BtnHeight);
    cs->setFixedSize(BtnWidth, BtnHeight);
    em->setFixedSize(BtnWidth, BtnHeight);
    fl->setFixedSize(BtnWidth, BtnHeight);
    ma->setFixedSize(BtnWidth, BtnHeight);
    psy->setFixedSize(BtnWidth, BtnHeight);
    hum->setFixedSize(BtnWidth, BtnHeight);
    mh->setFixedSize(BtnWidth, BtnHeight);

    // 添加按钮到布局中
    buttonLayout->addWidget(recommend);
    buttonLayout->addWidget(sea);
    buttonLayout->addWidget(cs);
    buttonLayout->addWidget(em);
    buttonLayout->addWidget(fl);
    buttonLayout->addWidget(ma);
    buttonLayout->addWidget(psy);
    buttonLayout->addWidget(hum);
    buttonLayout->addWidget(mh);

    // 设置按钮容器的固定高度，防止拉伸
    buttonContainer->setFixedHeight(BtnHeight);

    // 设置按钮样式：圆角、边框、背景颜色等
    QString buttonStyle = R"(
        QPushButton {
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            border-radius: 10px;
            font-size: 14px;
            color: #333;
        }
        QPushButton:hover {
            background-color: #e0e0e0;
        }
        QPushButton:pressed {
            background-color: #d0d0d0;
        }
    )";

    recommend->setStyleSheet(buttonStyle);
    sea->setStyleSheet(buttonStyle);
    cs->setStyleSheet(buttonStyle);
    em->setStyleSheet(buttonStyle);
    fl->setStyleSheet(buttonStyle);
    ma->setStyleSheet(buttonStyle);
    psy->setStyleSheet(buttonStyle);
    hum->setStyleSheet(buttonStyle);
    mh->setStyleSheet(buttonStyle);

    mainLayout->insertWidget(1, buttonContainer, 0, Qt::AlignLeft);

    blogListWidget = new BlogListWidget(this);

    mainLayout->addWidget(blogListWidget);

    setLayout(mainLayout);

    // 设置按钮激活样式
    QString activeStyle = R"(
        QPushButton {
            background-color: #4A90E2;
            border: 1px solid #357AE8;
            color: white;
            border-radius: 5px;
            font-size: 14px;
        }
    )";

    auto updateButtonStyles = [=](int currentIndex)
    {
        recommend->setStyleSheet(currentIndex == 0 ? activeStyle : buttonStyle);
        sea->setStyleSheet(currentIndex == 1 ? activeStyle : buttonStyle);
        cs->setStyleSheet(currentIndex == 2 ? activeStyle : buttonStyle);
        em->setStyleSheet(currentIndex == 3 ? activeStyle : buttonStyle);
        fl->setStyleSheet(currentIndex == 4 ? activeStyle : buttonStyle);
        ma->setStyleSheet(currentIndex == 5 ? activeStyle : buttonStyle);
        psy->setStyleSheet(currentIndex == 6 ? activeStyle : buttonStyle);
        hum->setStyleSheet(currentIndex == 7 ? activeStyle : buttonStyle);
        mh->setStyleSheet(currentIndex == 8 ? activeStyle : buttonStyle);
    };

    // 初始设置第一个按钮为激活状态
    updateButtonStyles(0);

    // 连接搜索框回车信号和搜索函数
    connect(searchBox, &QLineEdit::returnPressed, [=]()
            {
                QString keyword = searchBox->text();
                this->searchBlogs(keyword); // 假设 BlogListWidget 有提供搜索接口
            });

    // 连接按钮点击事件和 QStackedWidget 页面切换
    connect(recommend, &QPushButton::clicked, [=]()
            { 
                blogListWidget->load(DataAccess::getBlogListByHome());
                updateButtonStyles(0); });
    connect(sea, &QPushButton::clicked, [=]()
            { 
                blogListWidget->load(DataAccess::getBlogListBySea());
                updateButtonStyles(1); });
    connect(cs, &QPushButton::clicked, [=]()
            { 
                blogListWidget->load(DataAccess::getBlogListByHome());
                updateButtonStyles(2); });
    connect(em, &QPushButton::clicked, [=]()
            { 
                blogListWidget->clear();
                updateButtonStyles(3); });
    connect(fl, &QPushButton::clicked, [=]()
            {
                blogListWidget->clear();
                updateButtonStyles(4); });
    connect(ma, &QPushButton::clicked, [=]()
            { 
                blogListWidget->clear();
                updateButtonStyles(5); });
    connect(psy, &QPushButton::clicked, [=]()
            { 
                blogListWidget->clear();
                updateButtonStyles(6); });
    connect(hum, &QPushButton::clicked, [=]()
            { 
                blogListWidget->clear();
                updateButtonStyles(7); });
    connect(mh, &QPushButton::clicked, [=]()
            { 
                blogListWidget->clear();
                updateButtonStyles(8); });
    connect(blogListWidget, &BlogListWidget::openDetailPage, this, &HomePage::openDetailPage);

    blogListWidget->load(DataAccess::getBlogListByHome());
}

void HomePage::searchBlogs(const QString &keyword)
{
    if (keyword.isEmpty())
    {
        blogListWidget->load(DataAccess::getBlogListByHome());
    }
    else
    {
        blogListWidget->load(DataAccess::getBlogListBySearch());
    }
}